package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Controler.InformationsListener;
import Model.PropertiesUtils;

public class Informations extends JPanel {
	private static final int INFORMATIONS_WIDTH = Window.FRAME_WIDTH-600;
	private static final int INFORMATIONS_HEIGHT = Window.FRAME_HEIGHT;
	private static final long serialVersionUID = 1L;
	private JTextField player1Text;
	private JTextField player2Text;
	private JLabel score1Label;
	private JLabel score2Label;
	private JTextArea informations;
	private JPanel white;
	private JPanel black;
	

	public Informations(){		
		informations = new JTextArea();
		informations.setSize(new Dimension(INFORMATIONS_WIDTH,50));
		informations.setColumns(17);
		informations.setRows(3);
		informations.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		informations.setEditable(false);
		
		player1Text = new JTextField(PropertiesUtils.informationsProperties("playerName") + " 1");
		player1Text.setForeground(Color.BLACK);
		player1Text.setEditable(false);
		player1Text.setBackground(Color.WHITE);
		player1Text.setName("0");
		white = new JPanel();
		white.setPreferredSize(new Dimension(INFORMATIONS_WIDTH, (int)((INFORMATIONS_HEIGHT-50)/2)));
		white.setBackground(Color.WHITE);
		score1Label = new JLabel(PropertiesUtils.informationsProperties("score"));
		score1Label.setForeground(Color.BLACK);
		
		player2Text = new JTextField(PropertiesUtils.informationsProperties("playerName") + " 2");
		player2Text.setForeground(Color.WHITE);
		player2Text.setEditable(false);
		player2Text.setBackground(Color.BLACK);
		player2Text.setName("1");
		black = new JPanel();		
		black.setPreferredSize(new Dimension(INFORMATIONS_WIDTH, (int)((INFORMATIONS_HEIGHT-50)/2)));
		black.setBackground(Color.BLACK);
		score2Label = new JLabel(PropertiesUtils.informationsProperties("score"));
		score2Label.setForeground(Color.WHITE);
		
		this.setLayout(new GridBagLayout());
		white.setLayout(new GridBagLayout());
		black.setLayout(new GridBagLayout());
		GridBagConstraints constraintes = new GridBagConstraints();
		constraintes.anchor = GridBagConstraints.CENTER;
		constraintes.gridy=0;
		white.add(player1Text, constraintes);
		constraintes.gridy=1;
		white.add(score1Label, constraintes);
		constraintes.gridy=0;
		black.add(player2Text, constraintes);
		constraintes.gridy=1;
		black.add(score2Label, constraintes);
		constraintes.gridx=0;
		constraintes.gridy=0;
		this.add(black,constraintes);
		constraintes.gridx=0;
		constraintes.gridy=1;
		this.add(informations, constraintes);
		constraintes.gridx=0;
		constraintes.gridy=2;
		this.add(white, constraintes);
		this.setPreferredSize(new Dimension(INFORMATIONS_WIDTH, Window.FRAME_HEIGHT));
	}

	public void setInformations(String message) {
		this.informations.setText(message);
		repaint();
	}
	
	public void addListenerToTextFields(MouseListener ml, KeyListener kl){
		this.player1Text.addMouseListener(ml);
		this.player1Text.addKeyListener(kl);
		this.player2Text.addMouseListener(ml);
		this.player2Text.addKeyListener(kl);
	}
	
	public void setPlayer1Name(String name){
		player1Text.setText(name);
	}
	
	public void setPlayer2Name(String name){
		player2Text.setText(name);
	}
	
	public void setScore1(int score){
		score1Label.setText(PropertiesUtils.informationsProperties("score") + score);
	}
	
	public void setScore2(int score){
		score2Label.setText(PropertiesUtils.informationsProperties("score") + score);
	}
	
	public void localPlayer(int player){
		if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player1"))){
			white.setBorder(BorderFactory.createLineBorder(Color.RED));
		}else if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player2"))){
			black.setBorder(BorderFactory.createLineBorder(Color.RED));
		}
	}

	public void removeListenerToTextFields(MouseListener ml, KeyListener kl) {
		this.player1Text.removeMouseListener(ml);
		this.player1Text.removeKeyListener(kl);
		this.player2Text.removeMouseListener(ml);
		this.player2Text.removeKeyListener(kl);
	}
}
